<template>
	<view>
		<view class="user">
			<text v-text="user.className"></text>
			<text v-text="user.name"></text>
		</view>
		<pickerModule :picker="item" v-for="(item, index) in picker" :key="index" @pickerChange="pickerChange"></pickerModule>
		<baseForm :column.sync="column" @submitFun="submitFun" :submitStatus="submitDisable" :button="button"></baseForm>
	</view>
</template>

<script>
import baseForm from '@/wxcomponents/form/form.vue';
import pickerModule from '@/wxcomponents/form/picker.vue';
import common from '@/libs/login.js';
import { format, dictCode } from '@/libs/common.js';
export default {
	data() {
		let that = this;
		return {
			user: '',
			button: {
				hide: false
			},
			picker: [
				{
					label: '日期',
					value: format('YY-mm-dd', new Date()),
					required: true,
					name: 'datePicker',
					mode: 'date',
					text: format('YY-mm-dd', new Date())
				},
				{
					label: '课程',
					value: '',
					required: true,
					mode: 'selector',
					name: 'courseName',
					range: '',
					text: '请选择课程',
					hidden: false
				},
				{
					label: '教师',
					value: '',
					required: true,
					disabled: true,
					name: 'teacherName',
					mode: 'selector',
					range: '',
					text: '请选择教师',
					hidden: false
				}
			],
			column: [],
			teacherClass: '',
			submitDisable: true,
			haveRecord: false,
			noInfo: false
		};
	},
	async onLoad() {
		this.user = await common.getStorage('user');
	},
	async onShow() {
		await this.getTeacherClass();
	},
	methods: {
		async getQuesList() {
			let list = await this.$http.get({
				url: '/edu/questionnaireConfigController/list',
				data: {
					courseCode: this.picker[1].value,
					jobNumber: this.picker[2].value,
					submitDate: this.picker[0].value,
					studentNumber: ''
				}
			});
			this.column = list.result.map(item => {
				// let type = await dictCode('dict_questionnaire_type', item.questionType);
				if (item.questionType == 1) {
					return {
						label: `${item.name}`,
						name: item.name,
						type: 'normalInput',
						disabled: false,
						value: '',
						showIndex: true,
						configId: item.id,
						sortNum: item.sortNum,
						questionType: item.questionType
						// answerOptionType: '',
						// answerDesc: '',
					};
				} else {
					return {
						label: item.name,
						name: item.name,
						type: 'radio',
						disabled: false,
						value: 1,
						showIndex: true,
						// id: item.id,
						configId: item.id,
						sortNum: item.sortNum,
						questionType: item.questionType,
						// answerOptionType: '',
						// answerDesc: '',
						answerAType: item.answerAType,
						answerBType: item.answerBType,
						items: [
							{
								value: item.answerAType,
								name: item.answerAType == 1 ? '是' : '否'
							},
							{
								value: item.answerBType,
								name: item.answerBType == 1 ? '是' : '否'
							}
						]
					};
				}
			});
		},
		submitFun(e) {
			let answerSaveVoList = this.column.map(item => {
				return Object.assign(item, {
					answerOptionType: parseInt(item.value),
					answerDesc: item.value
				});
			});
			answerSaveVoList = JSON.stringify(answerSaveVoList);
			this.$http
				.post({
					url: '/edu/questionAnswer/save',
					data: {
						// answerSaveVoList:JSON.stringify(answerSaveVoList).replace(/\"/g, "'"),
						answerSaveVoStr: answerSaveVoList,
						courseName: this.picker[1].text,
						courseCode: this.picker[1].value,
						jobNumber: this.picker[2].value,
						submitDate: this.picker[0].value
					}
				})
				.then(e => {
					this.showToast('提交成功', () => {
						uni.navigateBack();
					});
				});
		},
		//根据日期获取当日的课程和教师
		async getTeacherClass(name, id) {
			let that = this;
			let resp = await this.$http.post({
				url: '/edu/questionAnswer/listCourseByStudentAndDate',
				data: {
					submitDate: that.picker[0].value
				}
			});
			if (resp.result.length) {
				this.teacherClass = resp.result;
				this.picker[1].hidden = false;
				this.picker[2].hidden = false;
				this.picker[1].range = await this.setFormList(this.teacherClass, 'courseName', 'courseCode');
				this.picker[2].range = await this.setFormList(this.teacherClass, 'teacherName', 'jobNumber');
			} else {
				this.picker[1].hidden = true;
				this.picker[2].hidden = true;
				this.picker[1].value = '';
				this.picker[1].text = '请选择课程';
				this.picker[2].value = '';
				this.picker[2].text = '请选择教师';
			}
		},
		//设置问卷问题
		async setFormList(teacherClass, name, id) {
			let arr = '';
			return (arr = teacherClass.map(item => {
				return {
					name: item[name],
					id: item[id]
				};
			}));
		},
		//改变选框
		async pickerChange(e) {
			this.column = [];
			this.button.hide = false;
			let findOne = this.picker.filter(item => {
				return item.name == e.name;
			})[0];
			if (findOne) {
				findOne.text = e.text;
				findOne.value = e.value;
				if (findOne.mode == 'selector') {
					let arr = e.range.map(item => item.id);
					let index = arr.indexOf(e.value);
					this.picker[2].value = this.picker[2].range[index].id;
					this.picker[2].text = this.picker[2].range[index].name;
				}
			}
			await this.getTeacherClass();
			if (this.picker[0].value && this.picker[1].value && this.picker[2].value) {
				this.checkReport();
			}
			this.checkFormStatus();
		},
		checkFormStatus() {
			let a = this.picker.every(item => {
				return item.value !== '';
			});
			let b = this.column.every(item => {
				return item.value !== '';
			});
			if (a && b) {
				this.submitDisable = false;
			} else {
				this.submitDisable = true;
			}
		},
		//获取评教记录
		async checkReport() {
			let that = this;
			let list = await this.$http.post({
				url: '/edu/questionAnswer/listByStudent',
				data: {
					courseCode: this.picker[1].value,
					jobNumber: this.picker[2].value,
					submitDate: this.picker[0].value,
					studentNumber: ''
				}
			});
			if (list.result.length !== 0) {
				let answerSaveVoStr = JSON.parse(list.result[0].answerSaveVoStr);
				that.column = answerSaveVoStr.map(item => {
					if (item.questionType == 1) {
						return {
							label: `${item.name}`,
							name: item.name,
							type: 'normalInput',
							disabled: true,
							value: item.answerDesc,
							showIndex: true,
							configId: item.id,
							sortNum: item.sortNum,
							questionType: item.questionType
							// answerOptionType: '',
							// answerDesc: '',
						};
					} else {
						return {
							label: item.name,
							name: item.name,
							type: 'radio',
							disabled: true,
							value: item.answerOptionType,
							configId: item.configId,
							sortNum: item.sortNum,
							questionType: item.questionType,
							answerOptionType: item.answerOptionType,
							answerDesc: '',
							questionType: dictCode('dict_questionnaire_type', item.questionType),
							answerAType: item.answerAType,
							answerBType: item.answerBType,
							items: [
								{
									value: item.answerAType,
									name: item.answerAType == 1 ? '是' : '否'
								},
								{
									value: item.answerBType,
									name: item.answerBType == 1 ? '是' : '否'
								}
							]
						};
					}
				});
				that.submitDisable = true;
				this.haveRecord = true;
				// this.picker[1].text =
				this.button.hide = true;
			} else {
				this.button.hide = false;
				this.getQuesList();
			}
		}
	},
	components: {
		baseForm,
		pickerModule
	},
	computed: {},
	watch: {
		column: {
			handler(n) {
				let check = n.every(item => {
					return n.value !== '';
				});
				if (!check) return;
				this.checkFormStatus();
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.user {
	text {
		font-size: 36upx;
		font-family: Source Han Sans CN;
		font-weight: bold;
		color: rgba(51, 51, 51, 1);
		display: inline-block;
		margin-right: 20upx;
	}

	padding: 50upx;
}
</style>
